package com.xy.shop.common.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @Description  WebMvcConfigurer
 * @Author jxp
 **/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

	/**
	 *  静态资源
	 * @param registry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
	}
	/**
	 * 处理跨浏览器请求
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")  // 拦截所有的url
				.allowedOrigins("*")    // 放行哪些原始域，比如"http://domain1.com,https://domain2.com"
				.allowCredentials(true) // 是否发送Cookie信息
				.allowedMethods("GET", "POST", "PUT", "DELETE") // 放行哪些原始域(请求方式)
				.allowedHeaders("*");   // 放行哪些原始域(头部信息)
		super.addCorsMappings(registry);
	}

//	/**
//	 * 添加拦截器
//	 */
//	@Override
//	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(commonInterceptor).addPathPatterns("/**");
//		super.addInterceptors(registry);
//	}

//    @Bean
//    public FilterRegistrationBean registFilter() {
//        FilterRegistrationBean registration = new FilterRegistrationBean();
//        registration.setFilter(new OpenEntityManagerInViewFilter());
//        registration.addUrlPatterns("/*");
//        registration.setOrder(1);
//        return registration;
//    }
}
